home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7738 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: news.iastate.edu!usenet
  2. From: Steve Lee <sjlee@iastate.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: const wasting space and slower?
  5. Date: Sun, 18 Feb 1996 22:12:43 -0800
  6. Organization: Iowa State University
  7. Message-ID: <312814DB.399F@iastate.edu>
  8. NNTP-Posting-Host: dial61.ppp.iastate.edu
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0b6b (Win16; I)
  13.  
  14. Hi,
  15.  
  16. I know this debate has been discussed numerous times on this newsgroup (whether to use const or 
  17. #define for constants in C++), but I've never seen an answer for this question I just thought of. 
  18.  I know one benefit of using const is that you get type checking.  But here is my question...
  19.  
  20. When you declare something like this:
  21.  
  22. const int MAX_FILES = 20;
  23.  
  24. doesn't the compiler reserve global, read-only memory for this variable?  Or does it just replace 
  25. instances in the source code where MAX_FILES is found and replaces it with 20, similar to a 
  26. preprocessor, but with typechecking?  Also, if the compiler does place it in memory, do 
  27. instructions then reference the memory, instead of immediate addressing mode?
  28.  
  29. If this is so, I think it wastes space (storage of constants) and is slower (direct addressing 
  30. mode vs. immediate addressing mode).
  31.  
  32. -- 
  33. Steve Lee
  34. Computer Engineering/Computer Science
  35. Iowa State University
  36. email -> sjlee@iastate.edu
  37. WWW   -> http://www.cs.iastate.edu/~sjlee/homepage.html
  38.